<template>
  <!-- <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
     <a-divider orientation="left">
       <b>{{ formTitle }}</b>
     </a-divider> -->
  <a-modal :visible="open" :title="formTitle" @cancel="onClose">
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="用车任务id" prop="vehicleId" >
        <a-input v-model="form.vehicleId" placeholder="请输入用车任务id" />
      </a-form-model-item>
      <a-form-model-item label="明细类型;字典-material_type" prop="type" >
      </a-form-model-item>
      <a-form-model-item label="发货设备" prop="device" >
        <a-input v-model="form.device" placeholder="请输入发货设备" />
      </a-form-model-item>
      <a-form-model-item label="发货数量" prop="count" >
        <a-input v-model="form.count" placeholder="请输入发货数量" />
      </a-form-model-item>
      <a-form-model-item label="单位" prop="unit" >
        <a-input v-model="form.unit" placeholder="请输入单位" />
      </a-form-model-item>
      <a-form-model-item label="尺寸" prop="size" >
        <a-input v-model="form.size" placeholder="请输入尺寸" />
      </a-form-model-item>
      <a-form-model-item label="删除标识;删除标志" prop="delFlag" v-if="formType === 1">
        <a-input v-model="form.delFlag" placeholder="请输入删除标识;删除标志" />
      </a-form-model-item>
    </a-form-model>
    <template slot="footer">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
    </template>
  </a-modal>
  <!-- </a-drawer> -->
</template>

<script>
import { getMaterial, addMaterial, updateMaterial } from '@/api/vehicle/material'

export default {
  name: 'CreateForm',
  props: {
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        vehicleId: null,

        type: null,

        device: null,

        count: null,

        unit: null,

        size: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        delFlag: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        device: [
          { required: true, message: '发货设备不能为空', trigger: 'blur' }
        ],

        count: [
          { required: true, message: '发货数量不能为空', trigger: 'blur' }
        ],

        delFlag: [
          { required: true, message: '删除标识;删除标志不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,

        vehicleId: null,

        type: null,

        device: null,

        count: null,

        unit: null,

        size: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        delFlag: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getMaterial(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateMaterial(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addMaterial(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
